package com.bigdata.mapreduce.topn;

import org.apache.hadoop.io.WritableComparator;

public class TGroupingComparator extends WritableComparator {

    public TGroupingComparator() {
        super(Tkey.class, true);
    }

    /**
     * 判断2个对象是否是一组
     *
     * @param a the first object to be compared.
     * @param b the second object to be compared.
     * @return 0表示是一组，非0不是一组
     */
    @Override
    public int compare(Object a, Object b) {
        // 相同年月的key分到一组
        Tkey k1 = (Tkey) a;
        Tkey k2 = (Tkey) b;

        if (k1.getYear() == k2.getYear() && k1.getMonth() == k2.getMonth()) {
            return 0;
        }
        // 不在一组，只要返回的不是0即可
        return 1;
    }
}
